package com.shuoyue.ycgk.ui.search;

import com.shuoyue.ycgk.base.BaseModel;
import com.shuoyue.ycgk.base.BasePresenter;
import com.shuoyue.ycgk.base.BaseResult;
import com.shuoyue.ycgk.base.BaseView;
import com.shuoyue.ycgk.base.ListWithPage;
import com.shuoyue.ycgk.entity.Course;
import com.shuoyue.ycgk.entity.HotSearch;
import com.shuoyue.ycgk.entity.News;
import com.shuoyue.ycgk.entity.PaperSet;
import com.shuoyue.ycgk.entity.RecruitmentItem;
import com.shuoyue.ycgk.entity.ShopItem;
import com.shuoyue.ycgk.net.RetrofitClient;
import com.shuoyue.ycgk.net.base.ApiSubscriber;
import com.shuoyue.ycgk.net.gsondefaultadapter.Optional;

import java.util.List;

import io.reactivex.Observable;

public interface SerchContract {
    interface View extends BaseView {
        void searchHotSuc(List<HotSearch> data);

        void searchRecuSuc(ListWithPage<RecruitmentItem> data);

        void searchNewsSuc(ListWithPage<News> data);

        void searchCourseSuc(ListWithPage<Course> data);

        void searchBookSuc(ListWithPage<ShopItem> data);

        void searchRealSuc(ListWithPage<PaperSet> data);
    }

    class Presenter extends BasePresenter<View> {
        Model model = new Model();


        public void searchHot(int modelId, int type) {
            apply(model.searHot(modelId == 0 ? null : modelId, type))
                    .subscribe(new ApiSubscriber<Optional<List<HotSearch>>>(mView, this) {
                        @Override
                        public void onNext(Optional<List<HotSearch>> optional) {
                            super.onNext(optional);
                            mView.searchHotSuc(optional.getIncludeNull());

                        }

                    });
        }

        public void searchRecuriment(String keyWord, int n) {
            apply(model.searchRecuriment(keyWord, n))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<RecruitmentItem>>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<ListWithPage<RecruitmentItem>> optional) {
                            super.onNext(optional);
                            mView.searchRecuSuc(optional.getIncludeNull());
                        }

                    });
        }

        public void searchCourse(String keyWord, int modelId, int n) {
            apply(model.searchCourse(keyWord, modelId, n))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<Course>>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<ListWithPage<Course>> optional) {
                            super.onNext(optional);
                            mView.searchCourseSuc(optional.getIncludeNull());

                        }

                    });
        }

        public void searchNews(String keyWord, int modelId, int n) {
            apply(model.searchNews(keyWord, modelId, n))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<News>>>(mView, this, true) {
                        @Override
                        public void onNext(Optional<ListWithPage<News>> optional) {
                            super.onNext(optional);
                            mView.searchNewsSuc(optional.getIncludeNull());

                        }

                    });
        }

        public void searchBooks(String keyWord, int modelId, int n) {
            apply(model.searchBooks(keyWord, modelId, n))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<ShopItem>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<ShopItem>> optional) {
                            super.onNext(optional);
                            mView.searchBookSuc(optional.getIncludeNull());
                        }
                    });
        }

        public void searchRealPaper(String keyWord, int n) {
            apply(model.searchRealPaper(keyWord, n))
                    .subscribe(new ApiSubscriber<Optional<ListWithPage<PaperSet>>>(mView, this) {
                        @Override
                        public void onNext(Optional<ListWithPage<PaperSet>> optional) {
                            super.onNext(optional);
                            mView.searchRealSuc(optional.getIncludeNull());
                        }
                    });
        }
    }

    class Model extends BaseModel {
        Observable<BaseResult<List<HotSearch>>> searHot(Integer modelId, int type) {
            return RetrofitClient.getInstance().getApi().searchHot(null, type);
        }

        Observable<BaseResult<ListWithPage<RecruitmentItem>>> searchRecuriment(String key, int page) {
            return RetrofitClient.getInstance().getApi().searchRecuriment(key, page);
        }

        Observable<BaseResult<ListWithPage<Course>>> searchCourse(String key, int modelId, int page) {
            return RetrofitClient.getInstance().getApi().searchCourse(key, modelId, page);
        }

        Observable<BaseResult<ListWithPage<News>>> searchNews(String key, int modelId, int page) {
            return RetrofitClient.getInstance().getApi().searchInformation(key, modelId, page);
        }

        Observable<BaseResult<ListWithPage<ShopItem>>> searchBooks(String key, int modelId, int page) {
            return RetrofitClient.getInstance().getApi().searchBook(key, modelId, page);
        }

        Observable<BaseResult<ListWithPage<PaperSet>>> searchRealPaper(String key, int page) {
            return RetrofitClient.getInstance().getApi().searchRealPaper(key, 1, page);
        }
    }
}
